import { dictUrl } from '../../api/index'
import { post, get, formatDate } from '../../utils/index'
import WxValidate from '../../utils/WxValidate'

Page({
  data: {
    form: {},
    formError: {},
    sexPopup: false,
    sexColumns: [{dictKey: 1, dictValue: '男'}, {dictKey: 2, dictValue: '女'}],
    birthPopup: false,
    graduationDatePopup: false,
    isStayPopup: false,
    isStayColumns: [{dictKey: 1, dictValue: '是'}, {dictKey: 2, dictValue: '否'}],
    yearPopup: false,
    yearColumns () {
      const yearColumns = []
      for(let i=1;i<50;i++) {
        yearColumns.push({dictKey: i, dictValue: `${i}年`})
      }
    return yearColumns
    },
    academicPopup: false,
    academicColumns: [],
    birthDate: new Date().getTime(),
    currentDate: new Date().getTime(),
    formatter (type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return `${value}日`
    }
  },
  async submit () { // 保存信息
    const { form, page } = this.data
    if (!this.WxValidate.checkForm(form)) {
      const formError = this.WxValidate.formError
      this.setData({ formError })
      wx.showToast({ title: '信息填写有误，请检查', icon: 'none' })
      return
    }
    const url = page === 'entry' ? entrySaveUrl : userUpdateeInfoUrl
    this.setData({ formError: {} })
    await post(url, form)
    wx.navigateBack({})
  },
  async loadAcademic () { // 学历类型
    const { data } = await get(dictUrl, { code: 'education_background' })
    this.setData({ academicColumns: data })
  },
  nameChange ({ detail }) { // 姓名
    this.setData({ 'form.name': detail })
  },
  schoolChange ({ detail }) { // 毕业院校
    this.setData({ 'form.school': detail })
  },
  majorChange ({ detail }) { // 专业
    this.setData({ 'form.major': detail })
  },
  phoneChange ({ detail }) { // 手机号
    this.setData({ 'form.phone': detail })
  },
  jobChange ({ detail }) { // 职位
    this.setData({ 'form.job': detail })
  },
  sexClick () { // 性别弹窗
    wx.hideKeyboard()
    this.setData({ sexPopup: true })
  },
  sexClose () { // 性别取消
    this.setData({ sexPopup: false })
  },
  sexConfirm ({ detail }) { // 性别确认
    const { value } = detail
    this.setData({
      'form.sex': value.dictKey,
      'form.sexName': value.dictValue
    })
    this.sexClose()
  },
  isStayClick () { // 厦门就业弹窗
    wx.hideKeyboard()
    this.setData({ isStayPopup: true })
  },
  isStayClose () { // 厦门就业取消
    this.setData({ isStayPopup: false })
  },
  isStayConfirm ({ detail }) { // 厦门就业确认
    const { value } = detail
    this.setData({
      'form.isStay': value.dictKey,
      'form.isStayName': value.dictValue
    })
    this.isStayClose()
  },
  yearClick () { // 工作经验弹窗
    wx.hideKeyboard()
    this.setData({ yearPopup: true })
  },
  yearClose () { // 工作经验取消
    this.setData({ yearPopup: false })
  },
  yearConfirm ({ detail }) { // 工作经验确认
    const { value } = detail
    this.setData({
      'form.year': value.dictKey,
      'form.yearName': value.dictValue
    })
    this.yearClose()
  },
  academicClick () { // 学历弹窗
    wx.hideKeyboard()
    this.setData({ academicPopup: true })
  },
  academicClose () { // 学历取消
    this.setData({ academicPopup: false })
  },
  academicConfirm ({ detail }) { // 学历确认
    const { value } = detail
    this.setData({
      'form.academic': value.dictKey,
      'form.academicName': value.dictValue
    })
    this.academicClose()
  },
  birthClick () { // 弹窗
    wx.hideKeyboard()
    this.setData({ birthPopup: true })
  },
  birthChange ({ detail }) { // 选择日期
    const birth = formatDate(new Date(detail), 'yyyy-MM-dd')
    this.setData({ 'form.birth': birth })
    this.birthClose()
  },
  birthClose () { // 关闭时间选择
    this.setData({ birthPopup: false })
  },
  graduationDateClick () { // 弹窗
    wx.hideKeyboard()
    this.setData({ graduationDatePopup: true })
  },
  graduationDateChange ({ detail }) { // 选择日期
    const graduationDate = formatDate(new Date(detail), 'yyyy-MM-dd')
    this.setData({ 'form.graduationDate': graduationDate })
    this.graduationDateClose()
  },
  graduationDateClose () { // 关闭时间选择
    this.setData({ graduationDatePopup: false })
  },
  initValidator () { // 初始化验证器
    const rules = {
      name: { required: true },
      sex: { required: true },
      birth: { required: true },
      school: { required: true },
      academic: { required: true },
      graduationDate: { required: true },
      major: { required: true },
      isStay: { required: true },
      phone: { required: true, tel: true },
      year: { required: true },
      job: { required: true, tel: true }
    }
    const messages = {
      name: { required: '请输入姓名' },
      sex: { required: '请选择性别' },
      birth: { required: '请选择出生年月' },
      school: { required: '请输入毕业院校' },
      academic: { required: '请选择学历' },
      graduationDate: { required: '选择毕业时间' },
      major: { required: '请输入所学专业' },
      isStay: { required: '请选择是否想再厦门就业' },
      phone: { required: '请输入手机号', tel: '请填写正确手机号' },
      year: { required: '请选择工作年限' },
      job: { required: '请输入现在职位' },
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  onLoad () {
    // this.loadAcademic()
    this.initValidator()
    const yearColumns = []
    for(let i=1;i<50;i++) {
      yearColumns.push({dictKey: i, dictValue: i})
    }
    this.setData({yearColumns})
  }
})